package mudus.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import mudus.GlobalSettings;

public class UserModel {

    Connection connection;

    public UserModel(Connection connection) {
        this.connection = connection;
    }

    public ResultSet getUserInfo(int userid) throws SQLException {
        String query = "SELECT id, username"
                + " FROM user"
                + " WHERE ID='"
                + userid
                + "'"
                + " LIMIT 1";        
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(query);
        return rs;
    }
    
    public ResultSet getUserInfo(String username) throws SQLException {
        String query = "SELECT id, username"
                + " FROM user"
                + " WHERE username='"
                + username
                + "'"
                + " LIMIT 1";       
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(query);
        return rs;
    }
    
    public ResultSet getUsrPwd(String username, String password) throws SQLException {
        String query = "SELECT * from user WHERE username = '" + username + "' AND password = md5('" + password + "')";
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(query);
        return rs;        
    } 
    
    public static void main(String[] args) throws SQLException {
        Connection con = DriverManager.getConnection(GlobalSettings.DB_URL);  
        UserModel userModel = new UserModel(con);
        ResultSet res = userModel.getUserInfo("hilmanbeyri");
        while(res.next()) {
            System.out.println("id : " + res.getString("id"));
        }
    }
}
